package com.cbg.controller;

import com.cbg.common.PageParam;
import com.cbg.pojo.TableIp;
import com.cbg.service.IIpService;
import com.cbg.service.IpServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by chenboge on 2016/12/10.
 */
@Controller
public class WelcomeController {

    @Resource
    HttpServletRequest request;

    @Resource
    IIpService mService;

    @RequestMapping("/welcome")
    public String welcome() {
        String currPageStr = request.getParameter("page");
        int currPage=1;
        if(null!=currPageStr&&!"".equals(currPageStr)){
            try {
                currPage = Integer.parseInt(currPageStr);
            }catch (Exception e){
            }

        }
        int rowCount = mService.getItemSize();
        PageParam pageParam = new PageParam();
        pageParam.setRowCount(rowCount);
        if(pageParam.getTotalPage()<currPage){
            currPage = pageParam.getTotalPage();
        }
        pageParam.setCurrPage(currPage);
        pageParam = mService.getPageIp(pageParam);
        request.setAttribute("pageParam", pageParam);
        return "welcome";
    }

    @RequestMapping("/hello")
    public void getAll() {

    }

    @RequestMapping("/test")
    public ResponseEntity<List<TableIp>> test() {
        return new ResponseEntity<List<TableIp>>(mService.getAll(), HttpStatus.OK);
    }


}
